import {Box, Dropdown} from '../components'
import {CurrentUser} from '../graphql'
import {
    concat,
    getCurrentUser,
    lensProp,
    MutationComponent,
    QueryComponent,
    set,
    setCurrentUser,
    setToken
} from '../util'
import GETACCOUNTALLUSERS from '../graphql/GetAccountAllUsers.graphql'
import Router from 'next/router'
import CHANGE_CURRENTUSER from '../graphql/ChangeCurrentUser.graphql'

const changeCurrentUser = async (idInfo, companies, mutate) => {
    const id = idInfo.split('|')[0];
    const userKind = idInfo.split('|')[1];
    const changeUser = companies.find(c => c.companyId == id || c.candidateId == id);

    const {data: {changeCurrentUser: {currentUser, token}}} = await  mutate({
        variables: {
            input: {
                companyId: userKind != 'CANDIDATE' ? changeUser.companyId : null,
                userKind,
                candidateId: userKind == 'CANDIDATE' ? changeUser.candidateId : null,
                accountId: getCurrentUser().accountId,
                companyName: userKind != 'CANDIDATE' ? changeUser.companyName : null,
                candidateName: userKind == 'CANDIDATE' ? changeUser.candidateName : null,
            }
        }
    })
    setToken(token)
    setCurrentUser({token, ...currentUser})
    if (userKind == 'CANDIDATE') {
        Router.push('/candidate/recruit-list')
    } else if (userKind == 'PLATFORM') {
        Router.push('/platform/exam-list')
    } else if(userKind == 'AGENCY'){
        Router.push('/agency/hire/list')
    }else {
        Router.push('/enterprise/demand/position-list')
    }
}

const ROLE = {
    'CANDIDATE': '个人',
    'ENTERPRISE': '企业',
    'AGENCY': '第三方',
    'PLATFORM': '平台'
}

const getUserList = (users, currentUser) => {
    let list = [];
    const userKind = currentUser.userKind;
    (users || []).map(x => {
        if (
            userKind == 'CANDIDATE'
                ? x.candidateId != currentUser.candidateId
                : x.companyId != currentUser.companyId
        ) {
            list.push({
                key: (x.companyId || x.candidateId) + '|' + x.userKind,
                name: (x.companyName || x.candidateName) + '(' + ROLE[x.userKind] + ')'
            })
        }
    })
    return list
}

export default () => (
    <Box>
        <QueryComponent
            gql={GETACCOUNTALLUSERS}
            render={({getAccountAllUsers}) => (
                <MutationComponent
                    gql={CHANGE_CURRENTUSER}
                    options={{
                        updateQueries: {
                            CurrentUser: (prev, {mutationResult: {data: {changeCurrentUser: {currentUser}}}}) => {
                                return set(lensProp('currentUser'), currentUser, prev);
                            }
                        }
                    }}
                    render={mutate => (
                        <CurrentUser render={({currentUser}) => (
                                <Dropdown onClick={({key}) => changeCurrentUser(key, getAccountAllUsers, mutate)}
                                          title={currentUser.name}
                                          items={getUserList(getAccountAllUsers, currentUser)}
                                />
                            )
                        }/>
                    )}/>
            )}
        />
    </Box>
)